--- title: Interactive Plots keywords: fastai sidebar: home_sidebar summary: "We use plotly for interactive plotting. Data for plotly is obtained using `create_rgb_lines`. Here `elements` are replaced by `ions` because main purpose of these plots is to create an interactive application where everything could be changed dynamically." description: "We use plotly for interactive plotting. Data for plotly is obtained using `create_rgb_lines`. Here `elements` are replaced by `ions` because main purpose of these plots is to create an interactive application where everything could be changed dynamically." nb_path: "InteractivePlots.ipynb" ---
{% raw %}
{% endraw %} {% raw %}

  Index 
  XmlElementTree 
  StaticPlots 
  InteractivePlots● 
  Utilities 
  StructureIO 
  Widgets 

{% endraw %} {% raw %}

get_rgb_data[source]

get_rgb_data(kpath=None, evals_set=None, pros_set=None, elements=[[0], [], []], orbs=[[0], [], []], interpolate=False, n=5, k=3, scale_color=False)

  • Returns a formatted RGB colored data to pass into rgb_to_plotly function. Two arguments, elements and orbs should be in one-to-one correspondence. Returned item has transpose data shape, so that main iteration is over bands.
  • Parameters
    • ax : Matplotlib axes object, if not given, linecollection is returned.
    • kapath : export_vasprun().kpath or get_kpts().kpath.
    • evals_set: export_vasprun().bands.evals or get_evals().evals. If calculations are spin-polarized, it will be ...evals.SpinUp/SpinDown for both. You need to apply twice for SpinUp and SpinDown separately.
    • pros_set : export_vasprun().pro_bands.pros or get_bands_pro_set().pros. If calculations are spin-polarized, it will be ...pros.SpinUp/SpinDown for both. You need to create collections twice for SpinUp and SpinDown separately.
    • elements : List three lists of ions to project on, each element could be range(start,stop,step) as well, remember that stop is not included in python. so range(0,2) will generate 0 and 1 indices.
    • orbs : List of three lists of orbitals indices. [[red],[green],[blue]], you can create any color by this combination. For example, to get s-orbital in yellow color, you will use [[0],[0],[]]. Do not remove empty list from there, it will not effect your orbital selection.
    • interpolate: Deafult is false, if True, it will add n points between nearest kpoints.
    • n : int, default is 5. Adds n points between nearest kpoints.
    • k : int, order of interpolation, defualt is 3. n > k should be hold.
    • scale_color: If True, colors are scaled to 1 at each points.
  • Returns
    • kpath : List of NKPTS. (interpolated if given.)
    • evals : An (NBAND,NKPTS) numpy arry.
    • colors : An (NBANDS,NKPTS,3) numpy array.
{% endraw %} {% raw %}
{% endraw %} {% raw %}

flip_even_patches[source]

flip_even_patches(array_1d, patch_length)

  • When you reshape bands data to 1D array, you may need to draw lines which do not link ends of plot, for that, it is required to flip patches, so that next band start from where 1st end and so one.
  • Parameters
    • array_1d : Numpy 1d array or list.
    • patch_length : length of xaxis patches, e.g NKPTS.
  • Returns
    • 1D list
  • Example

    k=[1,2,3,1,2,3] flip_even_patches(k,3) [1,2,3,3,2,1]

{% endraw %} {% raw %}
{% endraw %} {% raw %}

rgb_to_plotly[source]

rgb_to_plotly(rgb_data=None, mode='markers', max_width=5, showlegend=False, name='', labels=['s', 'p', 'd'], symbol=0)

  • Returns data object of plotly's figure using get_rgb_data. Returned data could be fed to a plolty's figure.
  • Parameters
    • rgb_data : output of get_rgb_data.
    • mode : Three plotting modes are available:
      • 'markers' : Plot whole data as a single scatter object. Its too fast.
      • 'bands' : Plot data such that each band is accessible via legend.
      • 'lines' : A replica of matplotlib LineCollection object. It plots at each point separately, slower than other two modes.
    • max_width : Line/Scatter thickness is scaled to max_width.
    • name : Name to be shown on hover text or legend.
    • labels : Optional, show red green blue colors corresponding orbitals.
    • showlegend : Optional, only suitbale if spin up/down or 'bands' mode is ON.
    • symbol : Plotly's marker symbol. 0 for circle, 5/6 for Up/Down.
{% endraw %} {% raw %}
{% endraw %} {% raw %}

plotly_to_html[source]

plotly_to_html(fig, filename=None, out_string=False)

  • Writes plotly's figure as HTML file or display in IPython which is accessible when online. It is different than plotly's fig.to_html as it is minimal in memory. If you need to have offline working file, just use fig.write_html('file.html') which will be larger in size.
  • Parameters
    • fig : A plotly's figure object.
    • filename : Name of file to save fig. Defualt is None and show plot in Colab/Online or return hrml string.
    • out_string: If True, returns HTML string, if False displays graph if possible.
{% endraw %} {% raw %}
{% endraw %} {% raw %}

plotly_rgb_lines[source]

plotly_rgb_lines(path_evr=None, elements=[[], [], []], orbs=[[], [], []], labels=['', '', ''], mode='markers', elim=[], E_Fermi=None, skipk=None, joinPathAt=[], max_width=6, title=None, xt_indices=[0, -1], xt_labels=['Γ', 'M'], figsize=None, interpolate=False, n=5, k=3)

  • Returns plotly's figure object, takes care of spin-polarized calculations automatically. elements,orbs and labels are required to be one-to-one lists of size 3 where each item in list could be another list or integer.
  • Parameters
    • path_ever : Path/to/vasprun.xml or xml output of read_asxml.
    • elements : List of size 3 of list of indices of ions. If not given, picks all ions for each orbital.
    • orbs : List of size 3 of list of orbital indices, if not gievn, s,p,d plotted.
    • labels : List of labels for projection.
    • mode : Three plotting modes are available:
      • 'markers' : Plot whole data as a single scatter object. Its too fast.
      • 'bands' : Plot data such that each band is accessible via legend.
      • 'lines' : A replica of matplotlib LineCollection object. It plots at each point separately, slower than other two modes.
    • **kwargs : interpolate, ticks, figsize,elim,joinPathAt,max_width,title etc.
{% endraw %} {% raw %}
{% endraw %}

This is a simple example of using plotly. It is slower than matplotlib but interactive.

{% raw %}
import os 
import pivotpy as pp 
from IPython.display import HTML
os.chdir('E:/Research/graphene_example/ISPIN_2/bands')
fig=pp.plotly_rgb_lines(mode='lines',elim=[-10,10],orbs=[[0],[1],[2,3]],labels=['s','p<sub>z</sub>','p<sub>x</sub>+p<sub>y</sub>'], xt_indices=[0,30,60,-1],xt_labels=['Γ','M','K','Γ'],E_Fermi=-2.7735)
HTML(fig.to_html())
{% endraw %}

Creating a grid plot using plotly_rgb_lines

  • Here is a workaround if you want to plot multiple axes like matplotlib. You can add traces to plotly's make_subplots object by getting data=plotly_rgb_lines().data and layout=plotly_rgb_lines().layout objects. See example below:
{% raw %}
import plotly.graph_objects as go 
from IPython.display import HTML
import pivotpy.i_plots as ips
import os 
from plotly.subplots import make_subplots

os.chdir('E:/Research/graphene_example/ISPIN_1/bands')
inds=[0,30,60,-1]
lbs=['Γ','M','K','Γ']
kargs=dict(mode='markers',elim=[-10,10],xt_indices=inds,xt_labels=lbs)
fig5=go.FigureWidget(make_subplots(rows=2, cols=2,shared_yaxes=True,shared_xaxes=True,
    horizontal_spacing=0.08,
    vertical_spacing=0.1,
    subplot_titles=['s','pz','py','s,p,d'],
    column_widths=[1,2],
    row_heights=[1,2],
    y_title='Energy (eV)'
                                  ))
[fig5.add_trace(trace,row=1,col=1) for trace in ips.plotly_rgb_lines(orbs=[0,[],[]],**kargs).data]
[fig5.add_trace(trace,row=1,col=2) for trace in ips.plotly_rgb_lines(orbs=[[],[1],[]],**kargs).data]
[fig5.add_trace(trace,row=2,col=1) for trace in ips.plotly_rgb_lines(orbs=[[],[],[2]],**kargs).data]
last=ips.plotly_rgb_lines(**kargs)
[fig5.add_trace(trace,row=2,col=2) for trace in last.data]
fig5.update_layout(width=600,height=400,font=last.layout.font,margin=dict(r=10,l=10,t=40,b=40))
fig5.update_xaxes(last.layout.xaxis)
last.layout.yaxis.title=''
fig5.update_yaxes(last.layout.yaxis)
HTML(fig5.to_html())
{% endraw %}

Click on gridplot.html to see the plot generated from above code.

Using collect_dos in Plotly

  • A function similar to quick_dos_lines is created in plotly below. Plotly will reduce number of parameters and you are able to tweek those parameters interactively in graph.
{% raw %}

plotly_dos_lines[source]

plotly_dos_lines(path_evr=None, elim=[], elements=[[0]], orbs=[[0]], labels=['s'], color_map='gist_rainbow', tdos_color=(0.5, 0.95, 0), linewidth=2, fill_area=True, vertical=False, E_Fermi=None, figsize=None, spin='both', interpolate=False, n=5, k=3, title=None)

  • Returns ax object (if ax!=False) and plot on which all matplotlib allowed actions could be performed, returns lists of energy,tdos and pdos and labels. If given,elements,orbs colors, and labels must have same length. If not given, zeroth ions is plotted with s-orbital.
  • Parameters)
    • path_evr : Path/to/vasprun.xml or output of export_vasprun. Auto picks in CWD.
    • elim : [min,max] of energy range.
    • E_Fermi : If not given, automatically picked from export_vasprun.
    • elements : List [[0,],] of ions indices, by defualt plot first ion's projections.
    • orbs : List [[0,],] lists of indices of orbitals, could be empty.
    • labels : List [str,] of orbitals labels. len(labels)==len(orbs) must hold.
    • color_map : Matplotlib's standard color maps. Default is 'gist_ranibow'. Use 'RGB' if want to compare with plotly_rgb_lines with 3 projection inputs (len(orbs)==3).
    • fill_area : Default is True and plots filled area for dos. If False, plots lines only.
    • vertical : False, If True, plots along y-axis.
    • figsize : Tuple in pixels (width,height).
    • interpolate: Default is False, if True, bands are interpolated.
    • n : int, number of points, default is 5.
    • k : int, order of interpolation 0,1,2,3. Defualt 3. n > k should be hold.
    • legend_kwargs: Dictionary to contain legend arguments to fix.
  • Returns
    • fig : Plotly's figure object.
{% endraw %} {% raw %}
{% endraw %} {% raw %}
import pivotpy as pp 
fig=pp.plotly_dos_lines(path_evr='E:/Research/graphene_example/ISPIN_2/dos/vasprun.xml',vertical=False,fill_area=True,elements=[[0],[1],[]],orbs=[0,1,[2,3]],labels=['C1-s','C2-p<sub>z</sub>','None-p<sub>x</sub>+p<sub>y</sub>'],figsize=(700,550))
pp.plotly_to_html(fig)
{% endraw %} {% raw %}

  Index 
  XmlElementTree 
  StaticPlots 
  InteractivePlots● 
  Utilities 
  StructureIO 
  Widgets 

{% endraw %}